package com.lino.design;

import com.sun.org.apache.xpath.internal.operations.Bool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.regex.Pattern;

/**
 * @description: 抽象模板
 * @author: lingjian
 * @createDate: 2023/2/10 9:06
 */
public abstract class NetMall {

    protected Logger logger = LoggerFactory.getLogger(NetMall.class);

    protected final Pattern p9 = Pattern.compile("(?<=title\\>).*(?=</title)");

    /**
     * 用户ID
     */
    String uId;
    /**
     * 用户密码
     */
    String uPwd;

    public NetMall(String uId, String uPwd) {
        this.uId = uId;
        this.uPwd = uPwd;
    }

    /**
     * 生成商品推广海报
     *
     * @param skuUrl 商品url地址
     * @return 推广海报
     */
    public String generateGoodsPoster(String skuUrl) {
        // 1.验证登录
        if (!login(uId, uPwd)) {
            return null;
        }
        // 2.爬虫商品
        Map<String, String> reptile = reptile(skuUrl);
        // 3.组装海报
        return createBase64(reptile);
    }

    /**
     * 模拟登录
     *
     * @param uId  用户ID
     * @param uPwd 用户密码
     * @return 登录结果
     */
    protected abstract Boolean login(String uId, String uPwd);

    /**
     * 爬虫提取商品信息(登录后的优惠价格)
     *
     * @param skuUrl 商品Url地址
     * @return 商品信息
     */
    protected abstract Map<String, String> reptile(String skuUrl);

    /**
     * 生成商品推广海报
     *
     * @param goodsInfo 商品信息
     * @return 推广海报
     */
    protected abstract String createBase64(Map<String, String> goodsInfo);
}
